package server

import (
	"github.com/gin-gonic/gin"
	"github.com/go-emix/emix-logrus"
	"time"
)

// 日志中间件
func LoggerMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		path := c.Request.URL.Path
		query := c.Request.URL.RawQuery
		method := c.Request.Method
		header := c.GetHeader("X-Forwarded-For")
		now := time.Now()
		c.Next()
		since := time.Since(now)
		if path != "/health" {
			emlogrus.InfoWith(emlogrus.Fields{"path": path, "query": query,
				"method": method, "ip": header, "cost": since})
		}
	}
}
